package com.bh.ly.oa.adapter;

import java.util.List;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.bh.ly.oa.R;
import com.bh.ly.oa.activity.OrderDetailContentViewActivity;

public class OrderDetailPageAdater extends PagerAdapter {
	List<String> list;
	Context context;

	public OrderDetailPageAdater(Context c, List<String> list) {
		this.list = list;
		this.context = c;
	}

	@Override
	public int getCount() {
		return 2;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;// 官方提示这样写
	}

	@Override
	public int getItemPosition(Object object) {
		return super.getItemPosition(object);
	}
	
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View)object);
	};

	List<JSONObject> array;

	public void setArray(List<JSONObject> array) {
		this.array = array;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		LayoutInflater inflater = LayoutInflater.from(this.context);

		View view = null;

		ListView orderListView;
		OrderDetailContentlistAdapter oderContentAdapter;

		ListView listView;
		View loadingView;
		TextView loadingFaildView;
		switch (position) {
		case 0:
			view = inflater.inflate(R.layout.order_detail_content_view, null);
			loadingView = view.findViewById(R.id.loading_view);
			loadingFaildView = (TextView) view.findViewById(R.id.loading_fiald_view);
			listView = ((ListView) view.findViewById(R.id.order_detail_content_view));

			oderContentAdapter = new OrderDetailContentlistAdapter(context);
			((OrderDetailContentlistAdapter) oderContentAdapter).setArray(array);
			listView.setAdapter(oderContentAdapter);
			listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
					Intent intent = new Intent(context, OrderDetailContentViewActivity.class);
					intent.putExtra("json", arg0.getAdapter().getItem(arg2).toString());
					context.startActivity(intent);
				}
			});

			Integer state = Integer.parseInt(list.get(position));
			switch (state) {
			case 0:// success
				loadingView.setVisibility(View.GONE);
				loadingFaildView.setVisibility(View.GONE);
				listView.setVisibility(View.VISIBLE);
				break;
			case 1:// 正在加载
				loadingView.setVisibility(View.VISIBLE);
				loadingFaildView.setVisibility(View.GONE);
				listView.setVisibility(View.GONE);
				break;
			case 2:// 加载失败
				loadingView.setVisibility(View.GONE);
				loadingFaildView.setVisibility(View.VISIBLE);
				listView.setVisibility(View.GONE);
				break;
			}
			oderContentAdapter.notifyDataSetChanged();
			break;
		case 1:
			view = inflater.inflate(R.layout.order_detail_view, null);
			orderListView = ((ListView) view.findViewById(R.id.order_detail));
			orderListView.setAdapter(new OrderDetailAdapter(this.context, list.get(position)));
			break;
		}
		container.addView(view, 0);

		return view;
	}

}
